package com.chang.bootplus.base.utils;


import com.google.common.base.Function;
import com.google.common.base.Predicate;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class StreamUtil {
    /**
     * https://stackoverflow.com/questions/23699371/java-8-distinct-by-property
     * @param keyExtractor
     * @param <T>
     * @return
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }
}
